package com.yuanfud.user.controller;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yuanfud.common.exception.RestException;
import com.yuanfud.common.util.ErrorCode;
import com.yuanfud.common.util.IPUtil;
import com.yuanfud.common.util.JwtHandler;
import com.yuanfud.common.util.ResultBody;
import com.yuanfud.entity.user.Token;
import com.yuanfud.entity.user.User;
import com.yuanfud.user.service.LoginService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.lang.JoseException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.logging.Logger;

@RestController
public class LoginController {
    private static org.slf4j.Logger log= LoggerFactory.getLogger(LoginController.class);
    @Autowired
    LoginService loginService;
    @RequestMapping(value="/login",method = RequestMethod.POST,produces = "application/json")
    @ApiOperation("用户登录")
    public ResultBody<Token> ValidateLogin(@ApiParam("用户实体")@RequestBody User user, HttpServletRequest request) throws RestException {
        ResultBody<Token> resultBody=new ResultBody<>();
        Token token=null;
     //   try {
            token = loginService.getJwtToken(user, IPUtil.getIpAddr(request));
            resultBody.setCode(ErrorCode.SUCCESS_OK);
            resultBody.setMsg(ErrorCode.SUCCESS_OK_MSG);
            resultBody.setData(token);
//        }
//        catch (RestException e){
//            log.error("登陆错误："+e.getMessage());
//            throw new RestException(ErrorCode.SYS_CONTENT_ERROR,ErrorCode.SYS_CONTENT_ERROR_MSG);
//        }
        return  resultBody;
    }

}
